package com.i2bt.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.NameValuePair;

import java.io.IOException;
import java.util.List;

/**
 * 
 * Title : HttpPost异步类
 *
 * Description : 通过http工具类的post访问传过来的url,并可以选择是否返回响应
 *
 * CreatedTime : 2014-8-10
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class HttpPostTask extends AsyncTask<String, Void, String>{
	
	private Context context;
	
	private String url;		//访问的网址
	
	private List<NameValuePair> params;		//post里面的参数数组
	
	private String param;		//json参数

	private ProgressDialog progress;		//进度对话框
	
	private boolean isResponse = true;		//是否返回响应值
	
	private String error;		//错误信息
	
	private OnAsyncTaskListener asyncTaskListener;		//异步任务监听器
	
	private boolean isSilent;		//是否不出现提示框
	
	@SuppressWarnings("unused")
	private HttpPostTask() {}
	
	public HttpPostTask(Context context, String url, List<NameValuePair> params, boolean isResponse) {
		this.context = context;
		this.url = url;
		this.params = params;
	}
	
	public HttpPostTask(Context context, String url, String param, boolean isResponse) {
		this.context = context;
		this.url = url;
		this.param = param;
	}
	
	public HttpPostTask(Context context, String url, String param, boolean isResponse, boolean isSilent) {
		this(context, url, param, isResponse);
		this.isSilent = isSilent;
	}
	
	@Override
	protected void onPreExecute() {
		//判断是否联网
		if (NetworkUtil.isNetworkConnection(context)) {
			if (!isSilent) {
				//创建进度对话框
				progress = new ProgressDialog((Activity)context);
				progress.setMessage("请稍等..");
				//不可按返回键取消
				progress.setCancelable(true);
				progress.show();
			}
		}
		else {
			error = "无网络连接!";
			cancel(false);
		}

		super.onPreExecute();
	}

	@Override
	protected String doInBackground(String... params) {
		//显示进度框

		String result = null;
		//访问网址逻辑
		
		
		try {
			HttpUtil httpUtil = new HttpUtil();
			result = httpUtil.postAndResponseAsyn(url, param);
//			if (isResponse) {
//				if (param == null) {
//					result = HttpUtil.postAndResponse(url, this.params);
//				}
//				else {
//					result = HttpUtil.postAndResponse(url, param);
//				}
//
//			}
//			else {
//				if (param == null) {
//					HttpUtil.post(url, this.params);
//				}
//				else {
//					HttpUtil.post(url, param);
//				}
//			}
		}
		catch (IOException e){
			e.printStackTrace();
			error = "连接超时!";
			cancel(false);
		}

		return result;
	}
	
	
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		//成功完成操作调用监听器的任务结果,并传入结果
		asyncTaskListener.taskResult(true, result);
		if (progress != null) {
			progress.dismiss();
		}
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
		//成功失误操作调用监听器的任务结果,并传入错误信息
		asyncTaskListener.taskResult(false, error);
		if (progress != null) {
			progress.dismiss();
		}
	}
	
	
	/**
	 * 设置监听器
	 * @param asyncTaskListener 监听器
	 */
	public void setOnAsyncTaskListener(OnAsyncTaskListener asyncTaskListener) {
		this.asyncTaskListener = asyncTaskListener;
	}
	
	
	
	/**
	 * 
	 * Title : HttpPost异步类监听器接口
	 *
	 * Description : 包含了一个结果方法, 当完成操作后就会调用
	 *
	 * CreatedTime : 2014-8-10
	 *
	 * @author wuliao & kakaxi & yi
	 *
	 * @version 1.0
	 *
	 */
	
	public interface OnAsyncTaskListener {
		/**
		 * 任务结果
		 * @param isSuc	是否成功
		 * @param result  结果信息
		 */
		public void taskResult(boolean isSuc, String result);
	}
	
}
